/**
 * Copyright (C) 2024-2030 Super BilGen & 超亿时代信息技术股份有限公司.All Rights Reserved.
 */
package cn.com.yang.ruleengine;

import com.ql.util.express.ExpressRunner;
import com.ql.util.express.DefaultContext;  
/**
 * @Model cn.com.yang.ruleengine.PromotionCalculator
 * @Desc  规则引擎 QLExpress
 * @author yangyijin
 * @since 2025年4月18日-上午9:18:08
 */
public class PromotionCalculator {
	
	public double calculatePromotedPrice(String promotionRule, double price) {  
        ExpressRunner runner = new ExpressRunner();  
        DefaultContext<String, Object> context = new DefaultContext<>();  
        context.put("price", price);  
 
        // 假设discount是从促销活动或用户信息中获取的，这里简化为硬编码  
        context.put("discount", 0.8); // 示例：8折优惠  
 
        try {  
            Object result = runner.execute(promotionRule, context, null, true, false);  
            return Double.parseDouble(result.toString());  
        } catch (Exception e) {  
            e.printStackTrace();  
            return price; // 如果执行规则出错，则返回原价  
        }  
    }  
	
	 // 根据动态的促销规则计算商品促销后的价格
    public static void main(String[] args) {  
        PromotionCalculator calculator = new PromotionCalculator();  
        double price = 150.0; // 商品原价  
        String promotionRule = "price * discount"; // 促销规则  
        double promotedPrice = calculator.calculatePromotedPrice(promotionRule, price);  
        System.out.println("促销后价格：" + promotedPrice);  
    }  
}
